package controller

import (
	"gin-ranking/api/auth/params"
	"gin-ranking/api/auth/service/impl"
	"gin-ranking/common/result"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"net/http"
	"strings"
)

type UserController struct{}

// Register 用户注册
func (u UserController) Register(c *gin.Context) {
	var param params.UserRegisterParam
	_ = c.BindJSON(&param)
	// 参数校验
	if validator.New().Struct(param) != nil {
		c.JSON(http.StatusOK, result.FailEnum(result.CodeMsg400))
		return
	}
	// 比较密码
	if param.Password != param.ConfirmPassword {
		c.JSON(http.StatusOK, result.FailEnum(result.CodeMsg1000))
		return
	}
	c.JSON(http.StatusOK, result.SuccessData(impl.UserServiceImpl{}.Register(param)))
}

// Login 用户登录
func (u UserController) Login(c *gin.Context) {
	var param params.UserLoginParam
	_ = c.BindJSON(&param)
	// 参数校验
	if validator.New().Struct(param) != nil {
		c.JSON(http.StatusOK, result.FailEnum(result.CodeMsg400))
		return
	}
	c.JSON(http.StatusOK, result.SuccessData(impl.UserServiceImpl{}.Login(param)))
}

// QueryPageList  获取用户分页列表
func (u UserController) QueryPageList(c *gin.Context) {
	var param params.UserQueryPageListParam
	_ = c.BindJSON(&param)
	c.JSON(http.StatusOK, result.SuccessData(impl.UserServiceImpl{}.QueryPageList(param)))
}

// QueryById  获取用户详情
func (u UserController) QueryById(c *gin.Context) {
	id := c.Param("id")
	c.JSON(http.StatusOK, result.SuccessData(impl.UserServiceImpl{}.QueryById(id)))
}

// QueryList  获取用户列表
func (u UserController) QueryList(c *gin.Context) {
	var param params.UserQueryListParam
	_ = c.BindJSON(&param)
	c.JSON(http.StatusOK, result.SuccessData(impl.UserServiceImpl{}.QueryList(param)))
}

// Add 添加用户
func (u UserController) Add(c *gin.Context) {
	var param params.UserAddParam
	_ = c.BindJSON(&param)
	// 参数校验
	if validator.New().Struct(param) != nil {
		c.JSON(http.StatusOK, result.FailEnum(result.CodeMsg400))
		return
	}
	c.JSON(http.StatusOK, result.SuccessData(impl.UserServiceImpl{}.Add(param)))
}

// Edit 用户编辑
func (u UserController) Edit(c *gin.Context) {

	var param params.UserEditParam
	_ = c.BindJSON(&param)
	// 参数校验
	if validator.New().Struct(param) != nil {
		c.JSON(http.StatusOK, result.FailEnum(result.CodeMsg400))
		return
	}
	impl.UserServiceImpl{}.Edit(param)
	c.JSON(http.StatusOK, result.Success())
}

// DeleteByIds 用户主键删除
func (u UserController) DeleteByIds(c *gin.Context) {
	var ids = strings.Split(c.Param("ids"), ",")
	impl.UserServiceImpl{}.DeleteByIds(ids)
	c.JSON(http.StatusOK, result.Success())
}
